package com.dmgis.qxfw.dataservice.businessservice.yydl.controller;

import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.DayLoad;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.DocFile;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.MsgMedium;
import com.dmgis.qxfw.dataservice.businessservice.common.util.*;
import com.dmgis.qxfw.dataservice.businessservice.dmgis_mongodb_srv.dataservice.service.Impl.MongoService1;
import com.dmgis.qxfw.dataservice.businessservice.yydl.service.DayForecastServiceI;
import com.dmgis.qxfw.dataservice.businessservice.yydl.service.DayPowerSerrviceI;
import com.dmgis.qxfw.dataservice.businessservice.yydl.service.HourPowerServiceI;
import com.dmgis.qxfw.dataservice.businessservice.yydl.service.Impl.UtilServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/xtdl")
public class DayPowerController {
	private  final String operatingSystem;

	@Autowired
	public DayPowerController(String operatingSystem) {
		this.operatingSystem = operatingSystem;
	}

	String temp = "";

	MongoService1 ms = new MongoService1();

	@Autowired
	private DayPowerSerrviceI dayPowerServiceImpl;

	@Autowired
	private DayForecastServiceI dayForecastServiceImpl;

	@Autowired
	private HourPowerServiceI hourPowerServiceImpl;

	@Autowired
	private UtilServiceImpl utilServiceImpl;

	@RequestMapping("/dayPowerLoad")
	@ResponseBody
	public String dayPowerLoad(HttpServletRequest request) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startTime = request.getParameter("DateTime");
		String sc = request.getParameter("sc");
		String powerType = request.getParameter("PowerType");
		List<DayLoad> list = new ArrayList<DayLoad>();
		list = dayPowerServiceImpl.dayPowerLoad(powerType, startTime);
		String tableData = "[";
		String xValue = "[";
		String yValue = "[";
		for (DayLoad dl : list) {
			String d = String.valueOf(Math.round(dl.getLoad() * 100) / 100);
			tableData += "{\"dateChar\":\"" + sdf.format(dl.getDate()) + "\",\"powerLoad\":\"" + d + "\"},";
			xValue += "'" + sdf.format(dl.getDate()).trim() + "',";
			yValue += d + ",";
		}
		if (tableData.endsWith(",")) {
			tableData = tableData.substring(0, tableData.length() - 1);
		}
		tableData = tableData + "]";
		if (xValue.endsWith(",")) {
			xValue = xValue.substring(0, xValue.length() - 1);
		}
		xValue = xValue + "]";
		if (yValue.endsWith(",")) {
			yValue = yValue.substring(0, yValue.length() - 1);
		}
		yValue = yValue + "]";
		yValue = "[{name:'"+DbConfig.CITY+"',data:" + yValue + "}]";
		temp = tableData + "|" + xValue + "|" + yValue + "|" + getCityFcst(startTime, sc);
		return temp;
	}

	// 初始化最大时间
	@RequestMapping("/initDayPowerLoadTime")
	@ResponseBody
	public String initDayPowerLoadTime() {

		Date dt = dayPowerServiceImpl.initDayPowerLoadTime();
		DateTime date = new DateTime(dt);
		String str = date.toDateString();
		return str;
	}

	@RequestMapping("/initDayPowerLoadTime2")
	@ResponseBody
	public String initDayPowerLoadTime2() {
		Date dt = dayPowerServiceImpl.initDayPowerLoadTime2();
		DateTime date = new DateTime(dt);
		String str = date.toDateString();
		return str;
	}

	// 获取澧县天气
	public String getCityFcst(String startTime, String sc) throws Exception {
		WebCtrlData wcd = new WebCtrlData();
		String strWeather = "";
		String result = "[";
		String maxResult = "[";
		double maxTemp = -65535, minTemp = -65535, maxHumid = -65535;
		List<Map<String, Object>> map = new ArrayList<Map<String, Object>>();
		if (DateTime.parseDate(startTime).AddDays(30).before(DateTime.now())) {
			map = dayForecastServiceImpl.getCityFcst(true, startTime);

		} else {
			map = dayForecastServiceImpl.getCityFcst(false, startTime);
		}
		if ((!map.isEmpty()) && map != null) {
			for (int i = 0; i < map.size(); i += 2) {
				Map<String, Object> m = map.get(i);
				String stationName = m.get("stationname").toString().trim();
				String weather1 = m.get("weather1").toString().trim();
				String weather2 = map.get(i + 1).get("weather1").toString().trim();
				String wcode1 = WebCtrlData.WeatherNameToPicPath(weather1);
				String wcode2 = WebCtrlData.WeatherNameToPicPath(weather2);
				String windS1 = wcd
						.GetWeatherFs(String.valueOf(WebCtrlData.FengSuToFengJi(Double.valueOf(m.get("winds").toString()))));
				String s = map.get(i + 1).get("winds").toString().trim();
				String windS2 = wcd.GetWeatherFs(String.valueOf(WebCtrlData.FengSuToFengJi(Double.valueOf(s))));
				String windD1 = wcd.GetAllFx(m.get("windd").toString().trim());
				String windD2 = wcd.GetAllFx(map.get(i + 1).get("windd").toString().trim());
				int index;
				if ((index = stationName.indexOf("乡")) > 0 || (index = stationName.indexOf("镇")) > 0) {
					stationName = stationName.substring(0, index + 1);
				}
				String maxTemp1 = m.get("maxtemp").toString();
				String maxTemp2 = map.get(i + 1).get("maxtemp").toString();
				String minTemp1 = m.get("mintemp").toString();
				String minTemp2 = map.get(i + 1).get("mintemp").toString();
				result += "{";
				result += "\"stationName\":\"" + stationName + "\",";
				result += "\"pic1\":\"" + wcode1 + "\",";
				result += "\"pic2\":\"" + wcode2 + "\",";
				double maxT = (Double.valueOf(maxTemp1) > Double.valueOf(maxTemp2) ? Double.valueOf(maxTemp1)
						: Double.valueOf(maxTemp2));
				if (maxTemp == -65535) {
					maxTemp = maxT;
				} else {
					maxTemp = (maxT > maxTemp ? maxT : maxTemp);
				}
				double minT = (Double.valueOf(minTemp1) > Double.valueOf(minTemp2) ? Double.valueOf(minTemp2)
						: Double.valueOf(minTemp1));
				if (minTemp == -65535) {
					minTemp = minT;
				} else {
					minTemp = (minT < minTemp ? minT : minTemp);
				}
				String maxHumid1 = m.get("humid").toString();
				String maxHumid2 = m.get("humid").toString();
				double maxH = (Double.valueOf(maxHumid1) > Double.valueOf(maxHumid2) ? Double.valueOf(maxHumid1)
						: Double.valueOf(maxHumid2));
				if (maxHumid == -65535) {
					maxHumid = maxH;
				} else {
					maxHumid = (maxH > maxHumid ? maxH : maxHumid);
				}
				result += "\"maxTemp\":\"" + (Double.valueOf(maxTemp1) > Double.valueOf(maxTemp2) ? maxTemp1 : maxTemp2)
						+ "\","; // 最高温度
				result += "\"minTemp\":\"" + (Double.valueOf(minTemp1) > Double.valueOf(minTemp1) ? minTemp2 : minTemp1)
						+ "\",";// 最低温度
				result += "\"windS1\":\"" + windS1 + "\",";
				result += "\"windS2\":\"" + windS2 + "\",";
				result += "\"windD1\":\"" + windD1 + "\",";
				result += "\"windD2\":\"" + windD1 + "\",";
				result += "\"tq1\":\"" + weather1 + "\",";
				result += "\"tq2\":\"" + weather2 + "\"";
				result += "},";
				if (!weather1.equals(weather2)) {
					strWeather = weather1 + "转" + weather2;
				} else {
					strWeather = weather1;
				}
			}
			if (result.endsWith(",")) {
				result = result.substring(0, result.length() - 1);
			}
			result = result + "]";
			maxResult += "{\"maxMaxTemp\":\"" + maxTemp + "\"," + "\"minMinTemp\":\"" + minTemp + "\","
					+ "\"maxHumid\":\"" + maxHumid + "\"," + "\"TEM\":\"" + strWeather + "\"}]";
			temp = result + "|" + maxResult;
		} else {
			result += "{";
			result += "\"stationName\":\"" + "\",";
			result += "\"pic1\":\"" + "\",";
			result += "\"pic2\":\"" + "\",";
			result += "\"maxTemp\":\"" + Math.round(maxTemp) + "\","; // 最高温度
			result += "\"minTemp\":\"" + Math.round(minTemp) + "\",";// 最低温度
			result += "\"windS1\":\"" + "\",";
			result += "\"windS2\":\"" + "\",";
			result += "\"windD1\":\"" + "\",";
			result += "\"windD2\":\"" + "\",";
			result += "\"tq1\":\"" + "\",";
			result += "\"tq2\":\"" + "\"";
			result += "},";
			result = result + "]";
			maxResult += "{\"maxMaxTemp\":\"" + Math.round(maxTemp) + "\"," + "\"minMinTemp\":\"" + Math.round(minTemp)
					+ "\"," + "\"maxHumid\":\"" + Math.round(maxHumid) + "\"," + "\"TEM\":\"" + strWeather + "\"}]";
			temp = result + "|" + maxResult;
		}

		return temp;
	}

	// 文档检索
	@RequestMapping("/fileDayPowerLoad")
	@ResponseBody
	public List<DocFile> fileDayPowerLoad(HttpServletRequest request) {
		String strStartTime = request.getParameter("StartTime") + " 00:00";
		String strEndTime = request.getParameter("EndTime") + " 23:59";
		List<DocFile> list = hourPowerServiceImpl.fileHourPowerLoad(strStartTime, strEndTime, "day");
		for (DocFile df : list) {
			String serviceFilePath = "";
			// 下载
			String fileName = df.getFileName();
			String collectionName = "";
			if(fileName.contains("doc")){
				collectionName = "doc_file";
			}else if(fileName.contains("pdf")){
				collectionName = "pdf_file";
			}
			String path = request.getContextPath();
			String basePath = DbConfig.IP + path
					+ "/Temp/PowerProduct/";
			String serviceUrl = basePath + fileName;
			String urlpath ;
			if (operatingSystem.equals("Windows")){
				 urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\PowerProduct\\";
			}else {
				 urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp/PowerProduct/";
			}

			serviceFilePath = urlpath + fileName;// 服务器文件
			FileUtil.mkDirs(urlpath);// 创建文件夹，生成多级目录
			FileUtil.ClearTempFiles(urlpath, 1000);// 清除缓存
			// 如果目录中不存在文件，下载雷达文件到temp目录
			File serviceFile = new File(serviceFilePath);// 服务器文件
			if (!serviceFile.exists()) {// 把文件上传到服务器
				Map<String, Object> queryMap = new HashMap<String, Object>();
				queryMap.put("id", df.getId());
				queryMap.put("table_name", "power_product");
				queryMap.put("advCode", DbConfig.advCode);
				queryMap.put("collectionName", collectionName);
				byte[] data = ms.selectData(queryMap);
				if (data == null) {
					serviceFilePath = "";
				} else {
					FileUtil.bytesToFile(data, serviceFilePath);
				}
			}
			if (!serviceFilePath.equals("")) {
				serviceFilePath = serviceUrl;
			}

			String productPath = serviceFilePath;
			//添加预报文档的路径路径
			df.setFilePath(productPath);
		}
		return list;
	}

	// 文档查看doc
	@RequestMapping("/showDayDoc")
	@ResponseBody
	public String ShowDoc(HttpServletRequest request) {
		String id = request.getParameter("id");// 获取编号
		String docpath = request.getParameter("Path");// 获取前台文档路径
		String urlpath ;
		if (operatingSystem.equals("Windows")){
			 urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\DayPowerLoad\\";// 临时文件目录
		}else {
			 urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp/DayPowerLoad/";// 临时文件目录
		}


		FileUtil.mkDirs(urlpath);// 创建文件夹，生成多级目录
		FileUtil.ClearTempFiles(urlpath, 1000);// 清除缓存

		String fileName = docpath.split("\\\\")[docpath.split("\\\\").length - 1];// 文件名
		String doctype = fileName.split("\\.")[fileName.split("\\.").length - 1];// 文件类型
		String servicePath = "";// 服务器文件
		if (doctype.contains("doc")) {
			String localPath = urlpath + fileName;
			String pdfPath = urlpath + fileName.replace(doctype, "pdf");
			File pdfFile = new File(pdfPath);
			if (pdfFile.exists()) {
				if (operatingSystem.equals("Windows")){
					servicePath = "Temp\\DayPowerLoad\\" + fileName.replace(doctype, "pdf");
				}else {
					servicePath = "Temp/DayPowerLoad/" + fileName.replace(doctype, "pdf");
				}

			} else {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", id);
				map.put("table_name", "power_product");
				map.put("collectionName", "doc_file");
				map.put("advCode", DbConfig.advCode);
				byte[] data = ms.selectData(map);
				FileUtil.bytesToFile(data, localPath);
				// 下载doc文件
				if (data == null) {
					return "";
				}
				File localFile = new File(localPath);// 本地文件
				if (localFile.exists()) {
					try {
						Word2PdfUtil.doc2pdf(localPath, pdfPath);
						if (operatingSystem.equals("Windows")){
							servicePath = "Temp\\DayPowerLoad\\" + fileName.replace(doctype, "pdf");
						}else {
							servicePath = "Temp/DayPowerLoad/" + fileName.replace(doctype, "pdf");
						}

					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		servicePath = servicePath.replace("\\", "/");
		return servicePath;
	}

	// txt查看
	@RequestMapping("/readTxt")
	@ResponseBody
	public String readTxt(HttpServletRequest request) throws IOException {
		String id = request.getParameter("id");// 获取编号
		String path = request.getParameter("Path");// 获取前台文档路径
		String urlpath ;
		if (operatingSystem.equals("Windows")){
			 urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\DayPowerTxt\\";// 临时文件目录
		}else {
			 urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp/DayPowerTxt/";// 临时文件目录
		}


		FileUtil.mkDirs(urlpath);// 创建文件夹，生成多级目录
		FileUtil.ClearTempFiles(urlpath, 1000);// 清除缓存

		String fileName = path.split("\\\\")[path.split("\\\\").length - 1];// 文件名
		String txttype = fileName.split("\\.")[fileName.split("\\.").length - 1];// 文件类型
		String result = "";// 服务器文件
		if (txttype.contains("txt")) {
			String txtPath = urlpath + fileName;
			File txtFile = new File(txtPath);
			if (txtFile.exists()) {
				result = FileUtil.readLogByStringAndEncode(txtPath, "GB2312");
			} else {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", id);
				map.put("table_name", "power_product");
				map.put("collectionName", "txt_file");
				map.put("advCode", DbConfig.advCode);
				byte[] data = ms.selectData(map);
				FileUtil.bytesToFile(data, txtPath);
				// 下载doc文件
				if (data == null) {
					return "";
				}
				result = FileUtil.readLogByStringAndEncode(txtPath, "GB2312");
			}
		}
		return result;
	}

	// 日总负荷校验
	@RequestMapping("/dayTotalPowerCheck")
	@ResponseBody
	public String dayTotalPowerCheck(HttpServletRequest request) throws Exception {
		Hashtable<String, String> tempMaxTable = new Hashtable<String, String>();// 实况最高温度
		Hashtable<String, String> tempMinTable = new Hashtable<String, String>();// 实况最低温度
		Hashtable<String, String> humidTable = new Hashtable<String, String>();// 实况湿度
		Hashtable<String, String> powerTable = new Hashtable<String, String>();// 实况电力负荷
		String dateTime = request.getParameter("dateTime");
		String dateTime2 = request.getParameter("endTime");
		List<DayLoad> list1 = new ArrayList<DayLoad>();
		// 电力预报
		List<DayLoad> list = dayPowerServiceImpl.getMaxPower(dateTime, dateTime2);
		List<MsgMedium> mList = new ArrayList<MsgMedium>();
		if (DateTime.parseDate(dateTime).AddDays(30).before(DateTime.now())) {
			mList = dayPowerServiceImpl.getMsgMedium(dateTime, dateTime2, true);
		} else {
			mList = dayPowerServiceImpl.getMsgMedium(dateTime, dateTime2, false);
		}
		// 电力实况
		list1 = dayPowerServiceImpl.getDayPowerList(dateTime, dateTime2);
		for (MsgMedium m : mList) {
			Date date = new Date();
			date = m.getObservTime();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			tempMaxTable.put(sdf.format(date), m.getDryBulbTemp());
			tempMinTable.put(sdf.format(date), m.getMinTemp());
			humidTable.put(sdf.format(date), m.getRelHumidity());
		}

		String ptd = "";
		for (DayLoad hp : list1) {
			DateTime d = new DateTime(hp.getDate());
			ptd = d.toDateTimeString();
			powerTable.put(ptd, String.valueOf(hp.getLoad()));
		}
		String tableData = "[";
		String xValue = "[";
		String yValue = "";
		String dlYbVal = "[";// 电力预报
		String dlSkVal = "[";// 电力实况
		String tempMaxVal = "[";// 最大温度实况
		String tempMinVal = "[";// 最小温度实况
		String humidVal = "[";// 湿度实况
		for (DayLoad d : list) {
			DateTime dt = new DateTime(d.getDate());
			String dateChar = dt.toDateTimeString();
			float dif = 0.0f;
			if (powerTable.get(dateChar) != null) {
				dif = d.getLoad() - Float.valueOf(powerTable.get(dateChar));
			}
			tableData += "{\"dateChar\":\"" + dateChar + "\",\"powerLoad\":\"" + d.getLoad() + "\",\"powerSk\":\""
					+ powerTable.get(dateChar) + "\",\"dif\":\"" + dif + "\"},";
			xValue += "\"" + dt.toDateString() + "\",";
			dlYbVal += Math.round(d.getLoad() * 100) / 100 + ",";
			dlSkVal += powerTable.get(dateChar) + ",";
			tempMaxVal += tempMaxTable.get(dateChar) + ",";
			tempMinVal += tempMinTable.get(dateChar) + ",";
			humidVal += humidTable.get(dateChar) + ",";
		}
		if (tableData.length() > 1) {
			tableData = tableData.substring(0, tableData.length() - 1);
		}
		tableData += "]";
		if (xValue.length() > 1) {
			xValue = xValue.substring(0, xValue.length() - 1);
		}
		xValue += "]";
		if (dlYbVal.length() > 1) {
			dlYbVal = dlYbVal.substring(0, dlYbVal.length() - 1);
		}
		dlYbVal += "]";
		if (dlSkVal.length() > 1) {
			dlSkVal = dlSkVal.substring(0, dlSkVal.length() - 1);
		}
		dlSkVal += "]";
		if (tempMaxVal.length() > 1) {
			tempMaxVal = tempMaxVal.substring(0, tempMaxVal.length() - 1);
		}
		tempMaxVal += "]";
		if (tempMinVal.length() > 1) {
			tempMinVal = tempMinVal.substring(0, tempMinVal.length() - 1);
		}
		tempMinVal += "]";
		if (humidVal.length() > 1) {
			humidVal = humidVal.substring(0, humidVal.length() - 1);
		}
		humidVal += "]";
		yValue = "[{\"name\":\"预报负荷\",\"data\":" + dlYbVal + "},{\"name\":\"实况负荷\",\"data\":" + dlSkVal
				+ "},{\"name\":\"最高温度\",\"data\":" + tempMaxVal + "},{\"name\":\"最低温度\",\"data\":" + tempMinVal
				+ "},{\"name\":\"最大湿度\",\"data\":" + humidVal + "}]";
		temp = tableData + "|" + xValue + "|" + yValue;
		return temp;
	}

	// 获取最新预报实况时间
	@RequestMapping("/getDayTotalPowerTime")
	@ResponseBody
	public String getDayTotalPowerTime() {
		Date date = dayPowerServiceImpl.getDayTotalPowerTime();
		DateTime dt = new DateTime(date);
		temp = dt.AddDays(-7).toDateString();
		temp += "|" + dt.toDateString();
		return temp;
	}

	/* 导出excel */
	@RequestMapping("/exportExcel")
	@ResponseBody
	public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 获取数据
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startTime = request.getParameter("DateTime");
		String sc = request.getParameter("sc");
		String powerType = request.getParameter("PowerType");
		List<DayLoad> list = new ArrayList<DayLoad>();
		list = dayPowerServiceImpl.dayPowerLoad(powerType, startTime);

		// excel标题
		String[] title = { "日期", "负荷" };

		// excel文件名
		String fileName = "日电力负荷预报表.xls";

		// sheet名
		String sheetName = "电力负荷预报表";

		String[][] content = new String[list.size()][];

		for (int i = 0; i < list.size(); i++) {
			content[i] = new String[title.length];
			DayLoad obj = list.get(i);
			SimpleDateFormat sdff = new SimpleDateFormat("yyyy-MM-dd");
			content[i][0] = sdff.format(obj.getDate());
			content[i][1] = String.valueOf(obj.getLoad());
		}

		// 创建HSSFWorkbook
		HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);

		// 响应到客户端
		try {
			this.setResponseHeader(response, fileName);
			OutputStream os = response.getOutputStream();
			wb.write(os);
			os.flush();
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/* 导出excel */
	@RequestMapping("/exportCheckExcel")
	@ResponseBody
	public void exportCheckExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 获取数据
		Hashtable<String, String> powerTable = new Hashtable<String, String>();// 实况电力负荷
		String dateTime = request.getParameter("dateTime");
		String dateTime2 = request.getParameter("endTime");
		List<DayLoad> list1 = new ArrayList<DayLoad>();
		// 电力预报
		List<DayLoad> list = dayPowerServiceImpl.getMaxPower(dateTime, dateTime2);
		// 电力实况
		list1 = dayPowerServiceImpl.getDayPowerList(dateTime, dateTime2);

		String ptd = "";
		for (DayLoad hp : list1) {
			DateTime d = new DateTime(hp.getDate());
			ptd = d.toDateTimeString();
			powerTable.put(ptd, String.valueOf(hp.getLoad()));
		}
		// excel标题
		String[] title = { "日期", "预报负荷", "实况负荷", "差值" };

		// excel文件名
		String fileName = "日电力负荷校验表.xls";

		// sheet名
		String sheetName = "电力负荷校验表";

		String[][] content = new String[list.size()][];

		for (int i = 0; i < list.size(); i++) {
			content[i] = new String[title.length];
			DayLoad obj = list.get(i);
			DateTime dt = new DateTime(obj.getDate());
			String dateChar = dt.toDateTimeString();
			float dif = 0.0f;
			if (powerTable.get(dateChar) != null) {
				dif = obj.getLoad() - Float.valueOf(powerTable.get(dateChar));
			}
			content[i][0] = dateChar;
			content[i][1] = String.valueOf(obj.getLoad());
			content[i][2] = powerTable.get(dateChar);
			content[i][3] = dif + "";
		}

		// 创建HSSFWorkbook
		HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);

		// 响应到客户端
		try {
			this.setResponseHeader(response, fileName);
			OutputStream os = response.getOutputStream();
			wb.write(os);
			os.flush();
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 发送响应流方法
	public void setResponseHeader(HttpServletResponse response, String fileName) {
		try {
			try {
				fileName = new String(fileName.getBytes(), "ISO8859-1");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			response.setContentType("application/octet-stream;charset=ISO8859-1");
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			response.addHeader("Pargam", "no-cache");
			response.addHeader("Cache-Control", "no-cache");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
